/**
 * 
 */
package com.example.android_is2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.example.android_is2.db.DBTracker;

/**
 * @author KAIN-PC
 *
 */
public class OpenTrackRouteActivity extends Activity implements OnItemLongClickListener{

  private DBTracker dbTracker = null;
  private ArrayList<TextView> listView = null;
  private AdapterListTextView adapterListView = null;
  private ListView listViewObj = null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opentrackroute);
    listView = new ArrayList<TextView>();
    // ListView
    adapterListView = new AdapterListTextView(listView, this);
    listViewObj = (ListView) findViewById(R.id.listViewOpenTrackRoute);
    listViewObj.setAdapter(adapterListView);
    listViewObj.setOnItemLongClickListener(this);

    dbTracker = new DBTracker(getApplicationContext());
    dbTracker.open();
    ArrayList<String> listTracciati = null;
    listTracciati = dbTracker.selectTracciati();
    if (listTracciati != null && !listTracciati.isEmpty())
    {
    for(int i = 0; i < listTracciati.size(); i++)
    {
      TextView tmp = new TextView(getApplicationContext());
      tmp.setText(listTracciati.get(i).toString());
      tmp.setTextSize(20);
      listView.add(tmp);
      listViewObj.setVisibility(0);
      adapterListView.notifyDataSetChanged();
      //Log.i(MainActivity.TAG+getLocalClassName(), "Itinerari: "+listItinerari.get(i));
    }
    }
    else
    {
      TextView tmp = new TextView(getApplicationContext());
      tmp.setText("Non ci sono tracciati");
      tmp.setTextSize(20);
      listView.add(tmp);
      adapterListView.notifyDataSetChanged();
    }
    dbTracker.open();
  }

  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
  }

  @Override
  protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
  }

  public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
    // TODO Auto-generated method stub
    dbTracker.open();
    String nameTracciato = listView.get(position).getText().toString();
    ArrayList<InfoGeoPoints> listTracciato = null;
    listTracciato = dbTracker.selectTracciato(nameTracciato);
    dbTracker.close();
    SingletonParametersBridge.getInstance().addParameter("openListTracciato", listTracciato);
    Intent intent = new Intent(this, SeeTrackRouteActivity.class);
    startActivity(intent);
    return true;
  }

}
